home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15955 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.logicon.com!newsmaster@klee
  2. From: kkolda@logicon.com (Kenneth D. Kolda)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Using the %c control in SCANF
  5. Date: 8 Apr 1996 21:11:44 GMT
  6. Organization: Logicon Operating Systems
  7. Message-ID: <4kbveg$6jh@piper.logicon.com>
  8. References: <4k9g0e$eom@knot.queensu.ca>
  9. NNTP-Posting-Host: 137.51.122.161
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.99.2
  12.  
  13. In article <4k9g0e$eom@knot.queensu.ca>, 3mb42@qlink.queensu.ca says...
  14. >
  15. >Can anyone please give me some examples of using the %c control in the 
  16. >scanf function.
  17. >
  18. >I keep getting segmentation faults when I use %c in the following line:
  19. >
  20. >        scanf ("%s%c", textLine);
  21. >
  22. >the variable "textLine" is declared as
  23. >
  24. >        char textLine[MAXBUFFERSIZE];
  25. >
  26. >while MAXBUFFERSIZE is an integer constant that equals 256.
  27. >
  28. >I am attempting to receive input, but not only up to the next non-blank 
  29. >character.  I want all of what is typed until the return key is hit.
  30. >
  31. >Can anyone help me use the %c control properly? If not, do you know of a 
  32. >better way of accomplishing this, perhaps with iostream.h functions 
  33. instead?
  34. >
  35. >Any help is appreciated, thanks. 
  36. >
  37. >M
  38. >
  39.  
  40. The segmentation fault is because you have two scan codes, %s and %c, but 
  41. only one object to write to (textLine).
  42.  
  43. A nice way to do this is using:
  44.  
  45.     cin.getline(textLine, MAXBUFFERSIZE);
  46.  
  47. This will read all input from stdin until a newline (up to a max of 
  48. MAXBUFFERSIZE -- a safety catch that your scanf does not have).  The only 
  49. thing to watch out for is that there are no newlines sitting in the stdin 
  50. buffer that haven't been read -- your cin.getline() will return having
  51. read 0 characters.  
  52.  
  53. Ken Kolda
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.